package gui;

import framework.Color;
import framework.GraphicsWrapper;

/**
 * Shows the current strength of the player's special ability
 * @author Jon Caddey
 *
 */
public class SpecialMeter {
	private static final Color FULL_COLOR = new Color(210, 225, 240);
	private static final Color EMPTY_COLOR = new Color(150, 210, 230);
	private static final int PADDING = 2;
	private int my_height;
	private int my_width;
	private int my_y;
	private int my_x;
	private double my_percent;
	private final int my_segmentCount;
	private double my_segmentLength;
	private Color my_color;
//	private final double my_padding = .1;
	// TODO implement segments with proper padding.
	
	public SpecialMeter(final int the_segmentCount) {
		my_segmentCount = the_segmentCount;
	}
	public void setSize(final int the_width, final int the_height) {
		my_height = the_height;
		my_width = the_width;
		my_segmentLength = (my_width - PADDING * (my_segmentCount - 1)) / my_segmentCount;
	}
	public void setPosition(final int the_x, final int the_y) {
		my_y = the_y;
		my_x = the_x;
	}
	public void render(GraphicsWrapper g) {
		final int fullSegments = (int)(my_percent * my_segmentCount / 100);
		g.setColor(my_color);
		double x = 0;
		for (int s = 0; s < fullSegments; s++) {
			g.fillRect((int)(my_x + x), my_y, (int)my_segmentLength, my_height);
			x += PADDING + my_segmentLength;
		}
		if (fullSegments != my_segmentCount) {
//			g.setColor(new Color(my_color.getRed(), my_color.getBlue(), my_color.getGreen(),
//					(int)(255 * ((my_segmentCount * my_percent / 100 - fullSegments)))));
//			g.fillRect((int)(my_x + x), my_y, (int)my_segmentLength, my_height);
			final int size = (int)(my_height * (my_segmentCount * my_percent / 100 - fullSegments));
			g.fillRect((int)(my_x + x), my_y + (my_height - size) / 2, (int)my_segmentLength, size);
		}
		//g.fillRect(my_x, my_y, (int)(my_width * my_percent / 100), my_height);
	}
	public void setPercent(final double the_percent) {
		my_percent = Math.max(0, the_percent);
		final double ratio = my_percent / 100;
		my_color = new Color(EMPTY_COLOR.getRed() + (int)(ratio * (FULL_COLOR.getRed() - EMPTY_COLOR.getRed())),
				EMPTY_COLOR.getGreen() + (int)(ratio * (FULL_COLOR.getGreen() - EMPTY_COLOR.getGreen())),
				EMPTY_COLOR.getBlue() + (int)(ratio * (FULL_COLOR.getBlue() - EMPTY_COLOR.getBlue())));
	}

}
